package com.nbicc.dp.web.widgetinstancevalue.dtoservice;

import com.nbicc.core.common.ResponseUtil;
import com.nbicc.core.db.modular.dp.widgetinstancevalue.imodel.AWidgetInstanceValue;
import com.nbicc.core.service.modular.dp.widgetinstancevalue.IWidgetInstanceValueService;
import com.nbicc.db.modular.dp.webpointvaluetype.model.WebPointValueType;
import com.nbicc.db.modular.dp.widgetinstancevalue.model.WidgetInstanceValueImpl;
import com.nbicc.dp.wsgateway.service.PublishWsPushSetServie;
import com.nbicc.service.modular.dp.webpointvaluetype.service.WebPointValueTypeService;
import com.nbicc.service.modular.dp.widgetinstancevalue.model.WidgetInstanceValuePackaging;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: lilingyan
 */
@Service
public class WidgetInstanceValueDtoService {

    @Resource(name = "MysqlWidgetInstanceValueService")
    private IWidgetInstanceValueService widgetInstanceValueService;
    @Autowired
    private PublishWsPushSetServie publishWsPushSetServie;

    @Autowired
    private WebPointValueTypeService webPointValueTypeService;

    public ResponseEntity<ModelMap> selectByWidgetClassId(Integer widgetClassId) {
        List<AWidgetInstanceValue> aWidgetInstanceValueList = widgetInstanceValueService.listByWidgetInstanceId(widgetClassId);
        List<WidgetInstanceValuePackaging> widgetInstanceValuePackagingList = new ArrayList<>();

        //查询出所有的值类型 并且把他们拼接到返回的控件值对象中(如果效率不够 则把这块放到前端)
        List<WebPointValueType> webPointValueTypeList = webPointValueTypeService.selectByAll();

        if(aWidgetInstanceValueList != null){
            for (AWidgetInstanceValue aWidgetInstanceValue : aWidgetInstanceValueList) {
                WidgetInstanceValuePackaging widgetInstanceValuePackaging = new WidgetInstanceValuePackaging();
                BeanUtils.copyProperties(aWidgetInstanceValue , widgetInstanceValuePackaging);
                for (WebPointValueType webPointValueType : webPointValueTypeList) {
                    if(webPointValueType.getId().equals(widgetInstanceValuePackaging.getWebPointValueTypeId())){
                        widgetInstanceValuePackaging.setWebPointValueType(webPointValueType);
                        break;
                    }
                }
                widgetInstanceValuePackagingList.add(widgetInstanceValuePackaging);
            }
        }
        return ResponseUtil.ok(widgetInstanceValuePackagingList);
    }

    public ResponseEntity<ModelMap> setAllWidgetInstanceValue(Integer widgetInstanceId, List<WidgetInstanceValueImpl> widgetInstanceValueList) {
        widgetInstanceValueService.setAllWidgetInstanceValue(widgetInstanceId , widgetInstanceValueList);
        return ResponseUtil.ok();
    }

    public ResponseEntity<ModelMap> updateAllWidgetInstanceValueById(Integer widgetInstanceId, List<WidgetInstanceValueImpl> widgetInstanceValueList) {
        widgetInstanceValueService.updateAllWidgetInstanceValueById(widgetInstanceId, widgetInstanceValueList);
        publishWsPushSetServie.widgetInstanceUpdate(widgetInstanceId);
        return ResponseUtil.ok();
    }

    public ResponseEntity<ModelMap> deleteByWidgetInstanceId(Integer widgetInstanceId) {
        widgetInstanceValueService.deleteByWidgetInstanceId(widgetInstanceId);
        return ResponseUtil.ok();
    }
}
